Skip to content

fix(api): handle empty string POSTHOG_KEY from Docker Compose - #1728

Merged
steebchen merged 1 commit into
mainfrom
ariana/karpathy-49db
Feb 23, 2026
Merged

steebchen merged 1 commit into
mainfrom
ariana/karpathy-49db

Conversation

@steebchen

@steebchen steebchen commented Feb 23, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Docker Compose sets POSTHOG_KEY to an empty string (not undefined) when the variable is unset on the host
  • The nullish coalescing operator (??) only catches null/undefined, so the empty string "" was passed to the PostHog constructor
  • PostHog's assert() throws because it requires a non-empty API key, even when disabled: true is set
  • Added a nonEmpty() helper to normalize empty strings to undefined before the ?? fallback

Test plan

  • CI test-split and test-unified should no longer crash with "You must pass your PostHog project's api key"
  • PostHog still works correctly when POSTHOG_KEY is properly set

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved PostHog initialization logic with enhanced handling of configuration values and fallback mechanisms to ensure more robust analytics service integration.

Docker Compose sets POSTHOG_KEY to an empty string (not undefined)
when the variable is unset on the host. The nullish coalescing operator
only catches null/undefined, so the empty string was passed to the
PostHog constructor which throws because it requires a non-empty key.
Normalize empty strings to undefined before applying the fallback.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 23, 2026 12:49
@coderabbitai

coderabbitai Bot commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

This change introduces a nonEmpty helper function to normalize empty environment variable strings to undefined, then uses it to compute posthogKey and posthogHost values. A posthogDisabled flag is derived from the presence of these variables, and PostHog initialization is adjusted to apply these computed values with fallback defaults.

Changes

Cohort / File(s) Summary
PostHog Initialization
apps/api/src/posthog.ts
Introduces nonEmpty helper to normalize empty env vars to undefined; computes posthogKey, posthogHost, and posthogDisabled values to conditionally initialize PostHog with fallback defaults when environment variables are absent or empty.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: handling empty string POSTHOG_KEY environment variables from Docker Compose. It is concise, specific, and directly reflects the core problem being solved.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ariana/karpathy-49db

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where the PostHog constructor would throw an assertion error when POSTHOG_KEY or POSTHOG_HOST environment variables are set to empty strings by Docker Compose. The fix adds a nonEmpty() helper function that normalizes empty strings to undefined, allowing the nullish coalescing operator to properly fall back to placeholder values.

Changes:

  • Added nonEmpty() helper to convert empty strings to undefined
  • Refactored PostHog initialization to use the helper before applying fallback values
  • Improved code comments explaining the Docker Compose empty string behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apps/api/src/posthog.ts
Comment on lines +6 to +11
function nonEmpty(value: string | undefined): string | undefined {
if (!value) {
return undefined;
}
return value;
}

Copilot AI Feb 23, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nonEmpty function uses a falsy check which correctly handles empty strings, but it won't catch whitespace-only strings like " " or "\t". Consider using .trim() before the check to ensure whitespace-only strings are also normalized to undefined. For example: if (!value?.trim()) { return undefined; }

Copilot uses AI. Check for mistakes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
apps/api/src/posthog.ts (1)

6-11: nonEmpty can be simplified to a one-liner

The four-line body is equivalent to return value || undefined.

♻️ Suggested simplification
 function nonEmpty(value: string | undefined): string | undefined {
-	if (!value) {
-		return undefined;
-	}
-	return value;
+	return value || undefined;
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/api/src/posthog.ts` around lines 6 - 11, The function nonEmpty currently
has a multi-line body; simplify it by returning the expression value ||
undefined directly in the function body so nonEmpty(value: string | undefined):
string | undefined { return value || undefined; }, keeping the same signature
and behavior and referencing the existing function name nonEmpty and its
parameter value.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@apps/api/src/posthog.ts`:
- Around line 6-11: The function nonEmpty currently has a multi-line body;
simplify it by returning the expression value || undefined directly in the
function body so nonEmpty(value: string | undefined): string | undefined {
return value || undefined; }, keeping the same signature and behavior and
referencing the existing function name nonEmpty and its parameter value.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc2f4ba and c60758d.

📒 Files selected for processing (1)
  • apps/api/src/posthog.ts

@steebchen
steebchen enabled auto-merge February 23, 2026 12:55
@steebchen
steebchen added this pull request to the merge queue Feb 23, 2026
Merged via the queue into main with commit 75534df Feb 23, 2026
12 checks passed
@steebchen
steebchen deleted the ariana/karpathy-49db branch February 23, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants